package com.kkwrite.regimen.provider.service.captcha;

import com.kkwrite.regimen.common.entity.dto.captcha.CaptchaDTO;
import com.kkwrite.regimen.common.entity.dto.captcha.CaptchaQuery;
import com.kkwrite.regimen.provider.domain.RegCaptchaDO;
import com.kkwrite.regimen.provider.repository.RegCaptchaRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 验证码服务实现类
 * 
 * @author Ke.Wang
 * @date 2018年7月19日 上午9:54:23
 */
@Service("captchaService")
public class CaptchaServiceImpl implements CaptchaService {

	private static final Logger logger = LoggerFactory.getLogger(CaptchaServiceImpl.class);
	
	@Resource(name = "regCaptchaRepository")
	private RegCaptchaRepository regCaptchaRepository;
	
	/**
	 * 获取验证码
	 */
	@Override
	public CaptchaDTO getCaptcha(CaptchaQuery captchaQuery) {
		if (logger.isDebugEnabled()) {
			logger.debug("CaptchaServiceImpl.getCaptcha(), captchaQuery = " + captchaQuery);
		}
		RegCaptchaDO regCaptchaDO = regCaptchaRepository.findByCaptchaSource(captchaQuery.getCaptchaSource());
		if (regCaptchaDO == null) {
			return null;
		}
		CaptchaDTO captchaDTO = new CaptchaDTO();
		BeanUtils.copyProperties(regCaptchaDO, captchaDTO);
		return captchaDTO;
	}

}
